/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.eproject.controler;

import aptech.eproject.DBConnection;
import aptech.eproject.entity.Bills;
import aptech.eproject.entity.Departments;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *
 * @author Ntienbo
 */
public class DepartmentsControler {

    DBConnection db;
    Connection conn;

    public DepartmentsControler() {
        db = new DBConnection();
    }

    public boolean insertDepartments(Departments d) {
        try {
            conn = db.getConnect();
            PreparedStatement statement = conn.prepareStatement("INSERT INTO Departments VALUES (?,?,?)", ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setInt(1, d.getDeptID());
            statement.setString(2, d.getDeptName());
            statement.setInt(3, d.getDeptStatus());
            statement.executeUpdate();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean deleteDepartments(Departments d) {
        try {
            PreparedStatement statement = conn.prepareStatement("DELETE Departments WHERE DeptID=?", ResultSet.CONCUR_UPDATABLE, ResultSet.TYPE_SCROLL_SENSITIVE);
            statement.setInt(1, d.getDeptID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }

    public boolean updateDepartments (Departments d) {
        try {
            PreparedStatement statement = conn.prepareStatement("UPDATE Departments SET "
                    + "(DeptName=?,DeptStatus=?)  WHERE DeptID=?");
            statement.setString(1, d.getDeptName());
            statement.setInt(2, d.getDeptStatus());
            statement.setInt(3, d.getDeptID());
            statement.executeUpdate();
            return true;
        } catch (SQLException ex) {
            return false;
        }

    }
}
